<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设施名称" prop="wtfName">
        <el-input
          v-model="queryParams.wtfName"
          placeholder="请输入设施名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施编号" prop="wtfCode">
        <el-input
          v-model="queryParams.wtfCode"
          placeholder="请输入设施编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施型号" prop="wtfModel">
        <el-input
          v-model="queryParams.wtfModel"
          placeholder="请输入设施型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="治理设施类型" prop="wtfType">
        <el-select v-model="queryParams.wtfType" placeholder="请选择治理设施类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="治理设施工艺" prop="wtfCraft">
        <el-input
          v-model="queryParams.wtfCraft"
          placeholder="请输入治理设施工艺"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用电量" prop="wtfEnergyConsumption">
        <el-input
          v-model="queryParams.wtfEnergyConsumption"
          placeholder="请输入用电量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药剂用量" prop="wtfMedicamentDosage">
        <el-input
          v-model="queryParams.wtfMedicamentDosage"
          placeholder="请输入药剂用量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="污染物进入量" prop="wtfWaster">
        <el-input
          v-model="queryParams.wtfWaster"
          placeholder="请输入污染物进入量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理量" prop="wtfProcess">
        <el-input
          v-model="queryParams.wtfProcess"
          placeholder="请输入处理量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理后的排放量" prop="wtfProcessEmission">
        <el-input
          v-model="queryParams.wtfProcessEmission"
          placeholder="请输入处理后的排放量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理后主要污染物的浓度" prop="wtfProcessPC">
        <el-input
          v-model="queryParams.wtfProcessPC"
          placeholder="请输入处理后主要污染物的浓度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新产生的污染物种类" prop="wtfNewMasterPType">
        <el-select v-model="queryParams.wtfNewMasterPType" placeholder="请选择新产生的污染物种类" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="wtfNewQuantity">
        <el-input
          v-model="queryParams.wtfNewQuantity"
          placeholder="请输入数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排放口名称" prop="wtfExhaustionHoleName">
        <el-input
          v-model="queryParams.wtfExhaustionHoleName"
          placeholder="请输入排放口名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="废水去向" prop="wtfWasteWaterDirection">
        <el-input
          v-model="queryParams.wtfWasteWaterDirection"
          placeholder="请输入废水去向"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年运行(小时)" prop="wtfYearTime">
        <el-input
          v-model="queryParams.wtfYearTime"
          placeholder="请输入年运行(小时)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年运行费用(万元)" prop="wtfYearMoney">
        <el-input
          v-model="queryParams.wtfYearMoney"
          placeholder="请输入年运行费用(万元)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投入使用日期" prop="wtfNewUseTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.wtfNewUseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择投入使用日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="环境监察记录" prop="wtfEnvRecord">
        <el-input
          v-model="queryParams.wtfEnvRecord"
          placeholder="请输入环境监察记录"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运行规程" prop="wtfRunRule">
        <el-input
          v-model="queryParams.wtfRunRule"
          placeholder="请输入运行规程"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设施平面布置图" prop="wtfFacilityPicture">
        <el-input
          v-model="queryParams.wtfFacilityPicture"
          placeholder="请输入设施平面布置图"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运行记录" prop="wtfRunRecord">
        <el-input
          v-model="queryParams.wtfRunRecord"
          placeholder="请输入运行记录"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业统一信用代码" prop="epId">
        <el-input
          v-model="queryParams.epId"
          placeholder="请输入企业统一信用代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['facilities:water:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['facilities:water:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['facilities:water:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['facilities:water:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设施名称" align="center" prop="wtfName"/>
      <el-table-column label="治理设施类型" align="center" prop="wtfType"/>
      <el-table-column label="治理设施工艺" align="center" prop="wtfCraft"/>
      <!--      <el-table-column label="用电量" align="center" prop="wtfEnergyConsumption" />-->
      <!--      <el-table-column label="药剂用量" align="center" prop="wtfMedicamentDosage" />-->
      <!--      <el-table-column label="污染物进入量" align="center" prop="wtfWaster" />-->
      <!--      <el-table-column label="处理量" align="center" prop="wtfProcess" />-->
      <!--      <el-table-column label="处理后的排放量" align="center" prop="wtfProcessEmission" />-->
      <!--      <el-table-column label="处理后主要污染物的浓度" align="center" prop="wtfProcessPC" />-->
      <!--      <el-table-column label="新产生的污染物种类" align="center" prop="wtfNewMasterPType" />-->
      <!--      <el-table-column label="数量" align="center" prop="wtfNewQuantity" />-->
      <!--      <el-table-column label="排放口名称" align="center" prop="wtfExhaustionHoleName" />-->
      <el-table-column label="废水去向" align="center" prop="wtfWasteWaterDirection"/>
      <el-table-column label="年运行(小时)" align="center" prop="wtfYearTime"/>
      <el-table-column label="年运行费用(万元)" align="center" prop="wtfYearMoney"/>
      <el-table-column label="投入使用日期" align="center" prop="wtfNewUseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.wtfNewUseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="环境监察记录" align="center" prop="wtfEnvRecord" />-->
      <!--      <el-table-column label="运行规程" align="center" prop="wtfRunRule" />-->
      <!--      <el-table-column label="设施平面布置图" align="center" prop="wtfFacilityPicture" />-->
      <!--      <el-table-column label="运行记录" align="center" prop="wtfRunRecord" />-->
      <!--      <el-table-column label="企业统一信用代码" align="center" prop="epId" />-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['facilities:water:edit']"
          >修改
          </el-button>
          <el-button
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['facilities:water:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改废水治理设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="app-container-atmosphere" width="70%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix" label-width="140px">
        <div class="content-left" style="margin-left: 4%">
          <div class="title2"><span> </span>基本信息</div>
          <el-form-item label="设施名称" prop="wtfName">
            <el-input v-model="form.wtfName" placeholder="请输入设施名称"/>
          </el-form-item>
          <el-form-item label="设施编号" prop="wtfCode">
            <el-input v-model="form.wtfCode" placeholder="请输入设施编号"/>
          </el-form-item>
          <el-form-item label="设施型号" prop="wtfModel">
            <el-input v-model="form.wtfModel" placeholder="请输入设施型号"/>
          </el-form-item>
          <el-form-item label="治理设施类型" prop="wtfType">
            <div style="width: 100%;">
              <el-select v-model="wtfIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="wtfChange">
                <el-option v-for="item in wtf" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="治理设施工艺" prop="wtfCraft">
            <el-input v-model="form.wtfCraft" placeholder="请输入治理设施工艺"/>
          </el-form-item>
          <el-form-item label="年用电量(千瓦)" prop="wtfEnergyConsumption">
            <el-input v-model="form.wtfEnergyConsumption" placeholder="请输入用电量"/>
          </el-form-item>
          <el-form-item label="药剂用量(吨)" prop="wtfMedicamentDosage">
            <el-input v-model="form.wtfMedicamentDosage" placeholder="请输入药剂用量"/>
          </el-form-item>
          <el-form-item label="污染物进入量" prop="wtfWaster">
            <el-input v-model="form.wtfWaster" placeholder="请输入污染物进入量"/>
          </el-form-item>
          <el-form-item label="年处理量(千克" prop="wtfProcess">
            <el-input v-model="form.wtfProcess" placeholder="请输入处理量"/>
          </el-form-item>
        </div>
        <div class="content-middle">
          <div class="title2"><span> </span>处理信息</div>
          <el-form-item label="处理后污染物浓度" prop="wtfProcessPC">
            <el-input v-model="form.wtfProcessPC" placeholder="请输入主要污染物浓度"/>
          </el-form-item>
          <el-form-item label="新生污染物种类" prop="wtfNewMasterPType">
            <el-input v-model="form.wtfNewMasterPType" placeholder="请输入新生污染物种类"/>
          </el-form-item>
          <el-form-item label="新生污染物数量" prop="wtfNewQuantity" type="number">
            <el-input v-model="form.wtfNewQuantity" placeholder="请输入新生污染物数量"/>
          </el-form-item>
          <el-form-item label="废水去向" prop="wtfWasteWaterDirection">
            <div style="width: 100%;">
              <el-select v-model="swdIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="swdChange">
                <el-option v-for="item in swd" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="年运行(小时)" prop="wtfYearTime" type="number">
            <el-input v-model="form.wtfYearTime" placeholder="请输入年运行(小时)"/>
          </el-form-item>
          <el-form-item label="年运行费用(万元)" prop="wtfYearMoney">
            <el-input v-model="form.wtfYearMoney" placeholder="请输入年运行费用(万元)"/>
          </el-form-item>
          <el-form-item label="投入使用日期" prop="wtfNewUseTime">
            <el-date-picker v-model="form.wtfNewUseTime" clearable
                            placeholder="选择投入使用日期"
                            size="small"
                            style="width: 100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="处理后排放量" prop="wtfProcessEmission">
            <el-input v-model="form.wtfProcessEmission" placeholder="请输入处理后排放量"/>
          </el-form-item>
        </div>
        <div class="content-right">
          <div class="title2"></div>
          <el-form-item label="环境监察记录" prop="wtfEnvRecord">
            <FileUpload v-model="form.wtfEnvRecord"/>
          </el-form-item>
          <el-form-item label="运行规程制度" prop="wtfRunRule">
            <FileUpload v-model="form.wtfRunRule"/>
          </el-form-item>
          <el-form-item label="设施平面布置图" prop="wtfFacilityPicture">
            <ImageUpload v-model="form.wtfFacilityPicture"/>
          </el-form-item>
          <el-form-item label="运行记录" prop="wtfRunRecord">
            <FileUpload v-model="form.wtfRunRecord"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center; margin-top: 30px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addWater, delWater, exportWater, getWater, listWater, updateWater} from "@/api/facilities/water";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Water",
  data() {
    return {
      wtf: [
        {
          value: '0',
          label: '工业废水'
        },
        {
          value: '1',
          label: '医疗废水'
        },
        {
          value: '2',
          label: '生活废水'
        },
        {
          value: '3',
          label: '沉淀池'
        }
      ],
      wtfIndex: '0',

      swd: [
        {
          value: '0',
          label: '自然流域(江河、湖、库)'
        },
        {
          value: '1',
          label: '城市下水道'
        },
        {
          value: '2',
          label: '城市污水处理厂'
        },
        {
          value: '3',
          label: '污灌农田'
        },
        {
          value: '4',
          label: '地渗或蒸发地'
        },
      ],
      swdIndex: '0',


      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 废水治理设施表格数据
      waterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wtfName: null,
        wtfCode: null,
        wtfModel: null,
        wtfType: null,
        wtfCraft: null,
        wtfEnergyConsumption: null,
        wtfMedicamentDosage: null,
        wtfWaster: null,
        wtfProcess: null,
        wtfProcessEmission: null,
        wtfProcessPC: null,
        wtfNewMasterPType: null,
        wtfNewQuantity: null,
        wtfExhaustionHoleName: null,
        wtfWasteWaterDirection: null,
        wtfYearTime: null,
        wtfYearMoney: null,
        wtfNewUseTime: null,
        wtfEnvRecord: null,
        wtfRunRule: null,
        wtfFacilityPicture: null,
        wtfRunRecord: null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wtfName: [
          {required: true, message: "设施名称不能为空", trigger: "blur"}
        ],
        wtfEnergyConsumption: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfMedicamentDosage: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfYearMoney: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfWaster: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfProcess: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfProcessPC: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        wtfNewQuantity: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    wtfChange(e) {
      this.form.wtfType = e;
    },
    swdChange(e) {
      this.form.wtfWasteWaterDirection = e;
    },
    /** 查询废水治理设施列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listWater(this.queryParams).then(response => {
            this.waterList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wtfId: null,
        wtfName: null,
        wtfCode: null,
        wtfModel: null,
        wtfType: null,
        wtfCraft: null,
        wtfEnergyConsumption: null,
        wtfMedicamentDosage: null,
        wtfWaster: null,
        wtfProcess: null,
        wtfProcessEmission: null,
        wtfProcessPC: null,
        wtfNewMasterPType: null,
        wtfNewQuantity: null,
        wtfExhaustionHoleName: null,
        wtfWasteWaterDirection: null,
        wtfYearTime: null,
        wtfYearMoney: null,
        wtfNewUseTime: null,
        wtfEnvRecord: null,
        wtfRunRule: null,
        wtfFacilityPicture: null,
        wtfRunRecord: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wtfId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加废水治理设施";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wtfId = row.wtfId || this.ids
      getWater(wtfId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改废水治理设施";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wtfId != null) {
            updateWater(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addWater(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wtfIds = row.wtfId || this.ids;
      this.$confirm('是否确认删除废水治理设施编号为"' + wtfIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delWater(wtfIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有废水治理设施数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportWater(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
<style lang="postcss" scoped>
.app-container-water {
  padding-left: 60px;
  padding-right: 60px;
}
</style>
